package de.nordakademie.wpk.boerse.ui.job;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import de.nordakademie.wpk.boerse.core.service.IBoerseService;
import de.nordakademie.wpk.boerse.ui.business.BoerseServiceManager;

public class RetriveStocksJob extends Job {

	public RetriveStocksJob(String name) {
		super(name);
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		monitor.beginTask("Abrufen der Aktiendaten", IProgressMonitor.UNKNOWN);
		IBoerseService service = BoerseServiceManager.getInstance()
				.getBoerseService();
		try {
			BoerseServiceManager.getInstance().setStocks(service.getStocks());
		} catch (final Exception e) {
			Display.getDefault().asyncExec(new Runnable() {
				@Override
				public void run() {
					MessageDialog.openError(new Shell(),
							"Fehler bei Abrufen der Aktiendaten",
							e.getMessage());
				}
			});
		}
		monitor.done();
		return Status.OK_STATUS;
	}
}
